package com.jonah.demo.hbase.thrift.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Properties;

public class PropertiesUtils {

    private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtils.class);

    private static String DEFAULT_PROPERTIES_FILE_NAME = "application.properties";

    private static volatile Properties properties = null;

    static {
        properties = getInstant();
    }

    // 获取值
    public static Object getValue(String key) {
        return properties.get(key);
    }

    public static Properties getInstant() {
        LOG.info("PropertiesUtils.getInstant start");
        if (properties == null) {
            synchronized (PropertiesUtils.class) {
                if (properties == null) {
                    properties = new Properties();
                    try {
                        properties.load(PropertiesUtils.class.getClassLoader().getResourceAsStream(DEFAULT_PROPERTIES_FILE_NAME));
                    } catch (IOException e) {
                        properties = null;
                        throw new RuntimeException(e.getMessage(), e);
                    }
                }
            }
        }
        LOG.info("PropertiesUtils.getInstant end");
        LOG.debug("PropertiesUtils instant values =>{}", properties);
        return properties;
    }

}
